package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.advanced;


import io.netty.channel.Channel;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelProxyFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.server.AbstractHandleSocksReportClientProxyServerTransferResponseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelFlowEnum;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelProtocolType;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelProxyType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.server.netty.flow.proxy.ServerChannelProxyFlow;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.handler.NettySocksClientProxyServerRealHandler;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;


@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleSocksReportClientProxyServerTransferResponseTypeAdvanced
        extends AbstractHandleSocksReportClientProxyServerTransferResponseTypeAdvanced<NettyProxyMsg> {


    private final ChannelProxyFlowAdapter channelProxyFlowAdapter;

    public ServerHandleSocksReportClientProxyServerTransferResponseTypeAdvanced(ChannelProxyFlowAdapter channelProxyFlowAdapter) {
        this.channelProxyFlowAdapter = channelProxyFlowAdapter;
    }

    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg       通道数据
     * @see NettySocksClientProxyServerRealHandler
     */
    @Override
    public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
        Channel channel = nettyChannelContext.channel();
        byte[] data = nettyProxyMsg.getData();
        Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);

        String targetIp = ChannelAttributeKeyUtils.getTargetIp(nextChannel);
        String clientId = ChannelAttributeKeyUtils.getClientId(nextChannel);
        Integer targetPort = ChannelAttributeKeyUtils.getTargetPort(nextChannel);
        if (nextChannel.isActive()) {
            // 将数据返回给客户端
            NettyProxyMsg nettyProxyMsgResponse = new NettyProxyMsg();
            nettyProxyMsgResponse.setType(ProxyMessageType.SOCKS_DISTRIBUTE_CLIENT_PROXY_SERVER_TRANSFER_RESPONSE_);
            nettyProxyMsgResponse.setData(data);

            nextChannel.writeAndFlush(nettyProxyMsgResponse);

            // 记录出口数据
            ServerChannelProxyFlow serverChannelFlow = ServerChannelProxyFlow
                    .builder()
                    .channelFlowEnum(ChannelFlowEnum.OUT_FLOW)
                    .ip(targetIp)
                    .port(targetPort)
                    .clientId(clientId)
                    .channelProxyType(ChannelProxyType.CLIENT_PROXY_SEVER)
                    .channelProtocolType(ChannelProtocolType.SOCKS)
                    .flow(nettyProxyMsg.getData().length)
                    .build();
            channelProxyFlowAdapter.asyncHandler(nextChannel, serverChannelFlow);
        } else {
            log.info("释放内存");
            ReferenceCountUtil.release(nettyProxyMsg);
        }
    }

}
